<?php


class News_model extends CI_Model {
    
//    $news = array('created' => '', 'Author' => '', 'title' => '', 'slug' => '', 'article' => '', 'aid' => '', 'public' => false, 'ntype' => [array('News, incident') ] 
//        'comments' => [array('cdate' => '', 'writer' => '', 'comment' => '')], 'archive' => true );

	public function __construct()
	{
	    parent::__construct();
            $this->load->helper('url');
            /// new MongoDate();
	}
        
        public function get_news($slug = FALSE )
    {
	if ($slug != FALSE)
	{
           $where = array('aid' => $_SESSION['curarea'], 'archive' => false, 'slug' => $slug);
	   $query = $this->mongo_db->where($where)
                          ->limit(1)
                          ->get('news');     
	}
        return $query;
    }
    
    public function getallnewsbyarea($aid, $ntype) {
        $where = array('aid' => $aid, 'archive' => false);
	$query = $this->mongo_db->where($where)->where_in('ntype', array($ntype))->get('news');
	return $query ;
    }


    public function set_news($news)
  {
        /*
         * Inserts a new news article
         */
	$this->load->helper('url');
        $slug = url_title($news['title'], 'dash', TRUE);
        $news = array('created' => new MongoDate(), 
                    'author' => $_SESSION['PublicName'], 
                    'title' => $news['title'], 
                    'slug' => $slug, 
                    'article' => $news['article'], 
                    'aid' => $_SESSION['curarea'], 
                    'public' => false, 
                    'ntype' => array('News'),
                    'archive' => false );
	
//	$slug = url_title($this->input->post('title'), 'dash', TRUE);
//	
//	$data = array(
//		'title' => $this->input->post('title'),
//		'slug' => $slug,
//		'text' => $this->input->post('text')
//	);
//	
	return $query = $this->mongo_db->insert('news', $news);
  }

function getnewsarray($data) {
    $news = [];
    foreach ($data as $sdata) {
    
    $newsitem = array('created' => date('d M Y H:i:s', $sdata['created']->sec) , 
        'author' => $sdata['author'],
        'title' => $sdata['title'],
        'slug' => $sdata['slug'], 
        'article' => $sdata['article'],
        'aid' => $sdata['aid'], 
        'public' => $sdata['public'],
        'ntype' => [$sdata['ntype']], 
        'comments' => [$sdata['comments']], 
        'archive' => $sdata['archive'] );
    $news = array_merge($news, $newsitem); 
    }
    print_r($news) ;   
    return $news;
  }
  
  
  
}

?>
